package cn.tedu.cwgl.controller;

import cn.tedu.cwgl.entity.Curaccount;
import cn.tedu.cwgl.entity.UserInfo;
import cn.tedu.cwgl.service.CuraccountService;
import cn.tedu.cwgl.util.Config;
import cn.tedu.cwgl.util.PageModel;
import cn.tedu.cwgl.util.Result;
import cn.tedu.cwgl.util.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class CuraccountControler {
    @Resource
    private CuraccountService curaccountService;

    @RequestMapping("/curaccount/getCuraccount/{currentPageNo}/{pageSize}")
    @ResponseBody
    public Result<Curaccount> getCuraccount(Curaccount curaccount, @PathVariable int currentPageNo,
                                            @PathVariable int pageSize, HttpSession session) {
        curaccount = getHousecuraccount(curaccount, session);
        PageModel model = new PageModel(currentPageNo, curaccount);
        model.setPageSize(pageSize);
        return curaccountService.getCuraccount(model);
    }

    private Curaccount getHousecuraccount(Curaccount curaccount, HttpSession session) {
        UserInfo user = Config.getSessionUser(session);
        if (user.getRoleid() == 2) {
            curaccount.setHouseid(user.getHouseid());
        } else if (user.getRoleid() == 3) {
            curaccount.setUserid(user.getId());
        }
        return curaccount;
    }

    @RequestMapping("/curaccount/delBill")
    @ResponseBody
    public Result delBill(Curaccount curaccount) {
        int result = curaccountService.delete(curaccount);
        if (result > 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.unSuccess();
        }
    }

    @RequestMapping("/curaccount/updateBill")
    @ResponseBody
    public Result updateBill(Curaccount curaccount) {
        int result = curaccountService.updateBill(curaccount);
        if (result > 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.unSuccess();
        }
    }

    @RequestMapping("/curaccount/addBill")
    @ResponseBody
    public Result addBill(Curaccount curaccount, HttpSession session) {
        int result = curaccountService.addBill(curaccount, session);
        if (result > 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.unSuccess();
        }
    }
}
